using System;
using Naptime.Dlc;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using Voodoo.Distribution;
using Voodoo.GameOps;
using Voodoo.Pattern;

public class BuyButton : MonoBehaviour
{
	public GameObject activeIcon;

	public Color tooExpensiveColor;

	public Color normalColor;

	public Image icon;

	public TextMeshProUGUI costText;

	public TextMeshProUGUI maxPurchaseText;

	public GameObject prestigeIcon;

	public GameObject ownedTxt;

	protected Item _item;

	public Button _button;

	public Action OnClicked;

	public TextMeshProUGUI titleTxt;

	[Header("RV")]
	public bool UseRV;

	[Header("Perks")]
	public Text fuel;

	public Text power;

	public Text size;

	public bool isPartOfDLC;

	public GameObject dlcBG;

	public Item Item => _item;

	public bool useGems(Item item)
	{
		if (item.ItemType == ItemType.Upgrade && Preloader.WallGemsConfig.IsEnabled)
		{
			return Preloader.WallGemsConfig.GemsValueMultiplier == 0f;
		}
		return false;
	}

	public void ShowActiveIcon()
	{
		activeIcon.SetActive(value: true);
	}

	public void HideActiveIcon()
	{
		activeIcon.SetActive(value: false);
	}

	public virtual void WatchRV()
	{
		if (useGems(_item))
		{
			InventoryManager.Instance.SpendGems((int)Math.Ceiling((decimal)((float)InventoryManager.Instance.GetPrice(_item) / 3f)));
		}
		else
		{
			InventoryManager.Instance.SpendMoney(InventoryManager.Instance.GetPrice(_item) / 3);
		}
		InventoryManager.Instance.UnlockItem(_item);
		OnClicked?.Invoke();
		SingletonMB<MessageCenter>.Instance.OnPermanentUpgrade?.Invoke();
	}

	public virtual void OnClick()
	{
		if (!useGems(_item))
		{
			if (InventoryManager.Instance.HasInventory(_item) && (float)_item.GetPrice(InventoryManager.Instance.GetInventoryCount(_item)) <= InventoryManager.Instance.CurrentBalance())
			{
				InventoryManager.Instance.SpendMoney(_item.GetPrice(InventoryManager.Instance.GetInventoryCount(_item)));
				InventoryManager.Instance.UnlockItem(_item);
				OnClicked?.Invoke();
				SingletonMB<MessageCenter>.Instance.OnPermanentUpgrade?.Invoke();
			}
		}
		else if (InventoryManager.Instance.HasInventory(_item) && _item.GetPrice(InventoryManager.Instance.GetInventoryCount(_item)) <= InventoryManager.Instance.CurrentGemsBalance())
		{
			InventoryManager.Instance.SpendGems(_item.GetPrice(InventoryManager.Instance.GetInventoryCount(_item)));
			InventoryManager.Instance.UnlockItem(_item);
			OnClicked?.Invoke();
			SingletonMB<MessageCenter>.Instance.OnPermanentUpgrade?.Invoke();
		}
	}

	private void ShowHideRV()
	{
		if (!Preloader.SkinsByRV.IsEnabled)
		{
			float num = (useGems(_item) ? ((float)InventoryManager.Instance.CurrentGemsBalance()) : InventoryManager.Instance.CurrentBalance());
			float num2 = (float)Math.Ceiling((decimal)((float)InventoryManager.Instance.GetPrice(_item) / 3f));
			if (InventoryManager.Instance.HasInventory(_item) && num > num2 && num < (float)InventoryManager.Instance.GetPrice(_item))
			{
				UseRV = true;
				useGems(_item);
			}
		}
	}

	public virtual void Fill(Item item, bool useCurrency = true, bool showLevel = false)
	{
		if ((bool)prestigeIcon)
		{
			prestigeIcon.SetActive(value: false);
		}
		UseRV = false;
		bool flag = Preloader.SkinsByRV.IsEnabled && item.ItemType == ItemType.Skin;
		_item = item;
		int price = item.GetPrice(InventoryManager.Instance.GetInventoryCount(item));
		if (useGems(item))
		{
			costText.text = item.GetPrice(InventoryManager.Instance.GetInventoryCount(item)) + " <sprite index= 0>";
		}
		else if (flag)
		{
			costText.text = item.RvPrice - InventoryManager.Instance.RvCountToUnlockItem(item) + "/" + item.RvPrice + " <sprite index= 1>";
		}
		else
		{
			costText.SetNumberTextMeshUGUI(price);
			if ((bool)prestigeIcon)
			{
				prestigeIcon.SetActive(value: true);
			}
		}
		if (item.ItemType == ItemType.Upgrade)
		{
			titleTxt.text = item.Name;
			if (item.GetPrice(InventoryManager.Instance.GetInventoryCount(item)) > SingletonMB<GameManager>.Instance.PrestigePoints)
			{
				costText.color = tooExpensiveColor;
			}
			else
			{
				costText.color = normalColor;
			}
		}
		else
		{
			titleTxt.text = item.Name;
			if (InventoryManager.Instance.HasInventory(_item))
			{
				dlcBG.SetActive(value: false);
				if (_item.GetPrice() > SingletonMB<GameManager>.Instance.PrestigePoints)
				{
					costText.color = tooExpensiveColor;
				}
				else
				{
					costText.color = normalColor;
				}
			}
			if (_item.isPartOfDLC)
			{
				isPartOfDLC = true;
				if (!Singleton<DLCManager>.Instance.IsSkinUnlocked(_item))
				{
					dlcBG.SetActive(value: true);
				}
				else
				{
					dlcBG.SetActive(value: false);
				}
			}
		}
		icon.sprite = item.Icon;
		costText.gameObject.SetActive(useCurrency || flag);
		if (Preloader.ShopConfig.ShopMissions && _item.ItemType != ItemType.Upgrade && !flag)
		{
			_button.interactable = !InventoryManager.Instance.HasInventory(_item);
		}
		maxPurchaseText.gameObject.SetActive(value: false);
		if (!InventoryManager.Instance.HasInventory(_item))
		{
			if (_item.ItemType == ItemType.Skin)
			{
				if (InventoryManager.Instance.CurrentlySelected(ItemType.Skin) == _item)
				{
					maxPurchaseText.text = "SELECTED";
				}
				else
				{
					maxPurchaseText.text = "OWNED";
				}
			}
			else
			{
				maxPurchaseText.text = "MAX";
			}
			maxPurchaseText.gameObject.SetActive(value: true);
			costText.gameObject.SetActive(value: false);
			if ((bool)prestigeIcon)
			{
				prestigeIcon.SetActive(value: false);
			}
		}
		if (useCurrency)
		{
			ShowHideRV();
		}
		if (item.ItemType == ItemType.Skin && Preloader.ShopConfig.SkinWithUniqueIncrement)
		{
			maxPurchaseText.text = Localization.GetTranslation("Generic_Unlocked");
			power.text = "+" + item.PowerPercent + "%";
			size.text = "+" + item.SizePercent + "%";
			fuel.text = "+" + item.FuelPercent + "%";
			if (item.PowerPercent > 0)
			{
				power.transform.parent.gameObject.SetActive(value: true);
			}
			if (item.SizePercent > 0)
			{
				size.transform.parent.gameObject.SetActive(value: true);
			}
			if (item.FuelPercent > 0)
			{
				fuel.transform.parent.gameObject.SetActive(value: true);
			}
		}
	}
}
